package com.example.neusoft.yuejian_12.Activity;

import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.example.neusoft.yuejian_12.Fragment.ClassFragment;
import com.example.neusoft.yuejian_12.Fragment.ClubInfoFragment;
import com.example.neusoft.yuejian_12.Fragment.ClubVideoFragment;
import com.example.neusoft.yuejian_12.JavaBean.Club;
import com.example.neusoft.yuejian_12.Listener.ClubCollectListener;
import com.example.neusoft.yuejian_12.Listener.ClubDetailListener;
import com.example.neusoft.yuejian_12.Listener.ClubStateListener;
import com.example.neusoft.yuejian_12.Listener.UserNoCollectListener;
import com.example.neusoft.yuejian_12.Model.ClubModel;
import com.example.neusoft.yuejian_12.Model.UserModel;
import com.example.neusoft.yuejian_12.R;
import com.squareup.picasso.Picasso;

public class ClubDetailActivity extends AppCompatActivity  implements View.OnClickListener  {
    private LinearLayout mClub;
    private LinearLayout mClass;
    private LinearLayout mVideo;
    private Fragment mTab6;
    private Fragment mTab7;
    private Fragment mTab8;
    private Club info;
    private Integer ClubInfo;
    private String id, userid, coresult;
    private  TextView name;
    private  ImageView pic,club_collect;
    private  ImageView back;
    private  String club_name,thumb;
   ClubDetailListener clubDetailListener=new ClubDetailListener() {
        @Override
        public void onResponse(Club listBean) {
            info = listBean;
            initData();
        }

        @Override
        public void onFail(String msg) {

        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_clubdetail);
        final Bundle bundle = getIntent().getExtras();
        id = bundle.getString("id");
        userid = getSharedPreferences("userInfo", Context.MODE_PRIVATE).getString("user_id", String.valueOf(0));
        Log.v("获取用户id", String.valueOf(userid));
        getClubState();
        ClubModel model = new ClubModel();
        model.getdetail(id, clubDetailListener);
        initViews();
        initEvents();
        setSelect(0);
    }

    public void initViews() {
        mClub = (LinearLayout) findViewById(R.id.clubinfo_club);
        mClass = (LinearLayout) findViewById(R.id.clubinfo_class);
        mVideo = (LinearLayout) findViewById(R.id.clubinfo_video);
        name = (TextView) findViewById(R.id.cname);
        pic = (ImageView) findViewById(R.id.cpic);
        back=(ImageView)findViewById(R.id.back);
        club_collect=(ImageView) findViewById(R.id.club_col);
    }

    public void initEvents() {
        mClub.setOnClickListener(this);
        mClass.setOnClickListener(this);
        mVideo.setOnClickListener(this);
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        club_collect.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                collect();
            }
        });
    }

    private void collect(){
        if("no".equals(coresult)){
            ClubModel model = new ClubModel();
            model.CollectClub(userid,id, clubColletListener);
            getClubState();
        } else{
            UserModel userModel = new UserModel();
            userModel.Nocollect(userid ,id,"1",usernocollectListener);

        }

    }

    ClubCollectListener clubColletListener=new ClubCollectListener() {
        @Override
        public void onResponse(Integer listBean) {
            Toast.makeText(ClubDetailActivity.this,"关注成功",Toast.LENGTH_LONG).show();
        }

        @Override
        public void onFail(String msg) {
        }
    };

    UserNoCollectListener usernocollectListener=new UserNoCollectListener() {
        @Override
        public void onResponse(Integer stringBean) {
            Toast.makeText(ClubDetailActivity.this,"取消关注成功",Toast.LENGTH_LONG).show();
            getClubState();
        }

        @Override
        public void onFail(String msg) {
            Toast.makeText(ClubDetailActivity.this,"取消关注失败",Toast.LENGTH_LONG).show();
        }
    };


    private void getClubState(){
        ClubModel model =new ClubModel();
        model.getClubState(id,userid,clubStateListener);

    }

        ClubStateListener clubStateListener =new ClubStateListener() {
            @Override
            public void onResponse(Integer listBean) {
                club_collect.setImageResource(R.drawable.ic_heart);
                coresult = "yes";

            }

            @Override
            public void onFail(String msg) {
                club_collect.setImageResource(R.drawable.ic_heart1);
                coresult = "no";

            }
        };



    public void initData() {
        club_name = info.getClub_name();
        thumb = info.getThumb();

        name.setText(club_name);
        if (thumb != "") {
            Picasso.with(ClubDetailActivity.this)
                    .load("http://172.24.10.175/workout/Uploads/" + thumb)
                    .into(pic);
            pic.setScaleType(ImageView.ScaleType.FIT_XY);
        } else {
            pic.setImageResource(R.drawable.qya);
        }
        getClubState();
    }
    private void setSelect(int i)
    {
        FragmentManager fm = getSupportFragmentManager();
        FragmentTransaction transaction = fm.beginTransaction();
        hideFragment(transaction);

        switch (i)
        {
            case 0:
                if (mTab6 == null)
                {
                    mTab6 = new ClubInfoFragment();
                    transaction.add(R.id.id_content1, mTab6);
                } else
                {
                    transaction.show(mTab6);
                }
                break;
            case 1:
                if (mTab7 == null)
                {
                    mTab7 = new ClassFragment()
                    ;transaction.add(R.id.id_content1, mTab7);
                } else
                {
                    transaction.show(mTab7);

                }
                break;
            case 2:
                if (mTab8 == null)
                {
                    mTab8 = new ClubVideoFragment();
                    transaction.add(R.id.id_content1, mTab8);
                } else
                {
                    transaction.show(mTab8);
                }
                break;
        }
        transaction.commit();
    }

    private void hideFragment(FragmentTransaction transaction)
    {
        if (mTab6 != null)
        {
            transaction.hide(mTab6);
        }
        if (mTab7 != null)
        {
            transaction.hide(mTab7);
        }
        if (mTab8 != null)
        {
            transaction.hide(mTab8);
        }
    }

    @Override
    public void onClick(View v)
    {
        switch (v.getId())
        {
            case R.id.clubinfo_club:
                setSelect(0);
                break;
            case R.id.clubinfo_class:
                setSelect(1);
                break;
            case R.id.clubinfo_video:
                setSelect(2);
                break;

            default:
                break;
        }
    }
}









